/*
 * PyPem.c
 *
 *  Created on: 2009-5-10
 *      Author: thh
 */
#include "base.h"
#include <openssl/bio.h>

PyMethodDef methods[]={
		{"read_rsa_private_key",(PyCFunction)PyPem_RSA_ReadPrivateKey,METH_VARARGS|METH_STATIC,""},
		{NULL}
};

PyTypeObject PyPem_Type={
		PyObject_HEAD_INIT(NULL)
		MODULE_NAME".Pem",
		sizeof(PyPem), 0,

		NULL,	//destructor tp_dealloc;
		NULL,	//printfunc tp_print;
		NULL,	//getattrfunc tp_getattr;
		NULL,	//setattrfunc tp_setattr;
		NULL,	//void *tp_reserved; /* formerly known as tp_compare */
		NULL,	//reprfunc tp_repr;

		/* Method suites for standard classes */

		NULL,	//PyNumberMethods *tp_as_number;
		NULL,	//PySequenceMethods *tp_as_sequence;
		NULL,	//PyMappingMethods *tp_as_mapping;

		/* More standard operations (here for binary compatibility) */

		NULL,	//hashfunc tp_hash;
		NULL,	//ternaryfunc tp_call;
		NULL,	//reprfunc tp_str;
		NULL,	//getattrofunc tp_getattro;
		NULL,	//setattrofunc tp_setattro;

		/* Functions to access object as input/output buffer */
		NULL,	//PyBufferProcs *tp_as_buffer;

		/* Flags to define presence of optional/expanded features */
		Py_TPFLAGS_DEFAULT,///long tp_flags;

		"",		//const char *tp_doc; /* Documentation string */

		/* Assigned meaning in release 2.0 */
		/* call function for all accessible objects */
		NULL,	//traverseproc tp_traverse;

		/* delete references to contained objects */
		NULL,	//inquiry tp_clear;

		/* Assigned meaning in release 2.1 */
		/* rich comparisons */
		NULL,	//richcmpfunc tp_richcompare;

		/* weak reference enabler */
		0,	//Py_ssize_t tp_weaklistoffset;

		/* Iterators */
		NULL,	//getiterfunc tp_iter;
		NULL,	//iternextfunc tp_iternext;

		/* Attribute descriptor and subclassing stuff */
		methods, //struct PyMethodDef *tp_methods;
		NULL,	//struct PyMemberDef *tp_members;
		NULL,	//struct PyGetSetDef *tp_getset;
		NULL,	//struct _typeobject *tp_base;
		NULL,	//PyObject *tp_dict;
		NULL,	//descrgetfunc tp_descr_get;
		NULL,	//descrsetfunc tp_descr_set;
		0,		//Py_ssize_t tp_dictoffset;
		NULL,	//initproc tp_init;
		NULL,	//allocfunc tp_alloc;
		NULL,	//newfunc tp_new;
		NULL,	//freefunc tp_free; /* Low-level free-memory routine */
		NULL,	//inquiry tp_is_gc; /* For PyObject_IS_GC */
		NULL,	//PyObject *tp_bases;
		NULL,	//PyObject *tp_mro; /* method resolution order */
		NULL,	//PyObject *tp_cache;
		NULL,	//PyObject *tp_subclasses;
		NULL,	//PyObject *tp_weaklist;
		NULL,	//destructor tp_del;

		/* Type attribute cache version tag. Added in version 2.6 */
		0,		//unsigned int tp_version_tag;
};

PyObject *PyPem_RSA_ReadPrivateKey(PyObject *self,PyObject *args)
{
	PyObject *obj;
	int fd;
	FILE *file;
	PyRSA *pyrsa = PyObject_NEW(PyRSA,PyRSA_Type);

	if(!PyArg_ParseTuple(args,"O",&obj))
		return NULL;
	file = fdopen(PyObject_AsFileDescriptor(obj),"rb");
	pyrsa->rsa = PEM_read_RSAPrivateKey(file,&pyrsa->rsa,"","");

	return pyrsa;
}
